package fm.card.entity;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.binary.Base64;


/**
 * 封装对外访问方法
 *
 * @author liuyazhuang
 */
public class WxCore {

    private static final String WATERMARK = "watermark";
    private static final String APPID = "appid";

    /**
     * 解密数据
     *
     * @return
     * @throws Exception
     */
    public static String decrypt(String appId, String encryptedData, String sessionKey, String iv) {
        String result = "";
        try {
            AES aes = new AES();
            byte[] resultByte = aes.decrypt(Base64.decodeBase64(encryptedData), Base64.decodeBase64(sessionKey), Base64.decodeBase64(iv));
            if (null != resultByte && resultByte.length > 0) {
                result = new String(WxPKCS7Encoder.decode(resultByte));
                JSONObject jsonObject = JSONObject.parseObject(result);
                String decryptAppid = jsonObject.getJSONObject(WATERMARK).getString(APPID);
                if (!appId.equals(decryptAppid)) {
                    result = "";
                }
            }
        } catch (Exception e) {
            result = "";
            e.printStackTrace();
        }
        return result;
    }


    public static void main(String[] args) throws Exception {
        String encryptedData = "r5Odcg2N7rKxMi81yqbXvlC+W08YoETHKU3jmC2pDla28PZy" +
                "tKH9ok+pavw1k6PrqUI76WTE92xNqiphYKkis19K1rh7z7xGV7226gsdjGUoeSLsiGP" +
                "fKxruVvSF8OOXN3O/Z/YMCjyF3Qjj7ZZUdAmDr9/dVJO9+Iq14iqza5cXYqIbXyj2xV5" +
                "kfWfh0GfZ7lMutwtbVqwqqrED91o7PkNFTr82ajiZb6oYGM43X8q+15YQyQJXtczdaM" +
                "SR3VdmrDpYG4eRqChP9X2QhFkwVy+Xzy/MRmPZ28LDg4Q3jpmhiq7OXvwLmHBpw0C6" +
                "FfdehwiuBsCcedbXr0+PpcmC6bK/7CTV42go3ejlb5gGanairvouUqU0ZqoURd0Ev5" +
                "N+OkYEpW8GWEh3eWloyiQbP67dZu2+Ib/" +
                "BPD/msx0nShRkzLSeImsJwN/PyHSRNYetDQm2ytLN/jfyjgz7HQXKtg==";
        String sessionKey = "081hBqLj0TkE1o1c8yLj01zHLj0hBqL-";
//        String iv = "8PmQhI2nSg4kuWbbAzP3NA==";
        String appId = "wx4f4bc4dec97d474b";
//        String encryptedData = "CiyLU1Aw2KjvrjMdj8YKliAjtP4gsMZMQmRzooG2" +
//                "xrDcvSnxIMXFufNstNGTyaGS9uT5geRa0W4oTOb1WT7fJl" +
//                "AC+oNPdbB+3hVbJSRgv+4lGOETKUQz6OYStslQ142dNCua" +
//                "bNPGBzlooOmB231qMM85d2/fV6ChevvXvQP8Hkue1poOFtn" +
//                "EtpyxVLW1zAo6/1Xx1COxFvrc2d7UL/lmHInNlxuacJXwu0" +
//                "fjpXfz/YqYzBIBzD6WUfTIF9GRHpOn/Hz7saL8xz+W//FRA" +
//                "Uid1OksQaQx4CMs8LOddcQhULW4ucetDf96JcR3g0gfRK4PC" +
//                "7E/r7Z6xNrXd2UIeorGj5Ef7b1pJAYB6Y5anaHqZ9J6nKEBvB" +
//                "4DnNLIVWSgARns/8wR2SiRS7MNACwTyrGvt9ts8p12PKFdlqYT" +
//                "opNHR1Vf7XjfhQlVsAJdNiKdYmYVoKlaRv85IfVunYzO0IKXsy" +
//                "l7JCUjCpoG20f0a04COwfneQAGGwd5oa+T8yO5hzuyDb/Xcx" +
//                "xmK01EpqOyuxINew==";
//        String sessionKey = "tiihtNczf5v6AKRyjwEUhQ==";
        String iv = "r7BXXKkLb8qrSNn05n0qiA==";
        System.out.println(encryptedData.length());
        System.out.println(decrypt(appId, encryptedData, sessionKey, iv));
    }
}